package th.co.exat.uap.model;

import com.channgam.dbmlib.sql.MySQLJDBC;
import com.channgam.dbmlib.sql.iDataAdapter;
import java.util.ArrayList;
import java.util.List;
import th.co.exat.uap.config.Config;

public class Car {

    private static iDataAdapter i;
    // Table CarType
    private String carTypeId;
    private String carTypeName;
    // Table Color
    private String colorId;
    private String colorName;
    // Table Brand
    private String brandId;
    private String brandName;
    // Table Province
    private String provinceId;
    private String provinceName;

    public static Car getProvinceById(String ProvinceId) throws Exception {
        Car c = new Car();
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();
        String sql = "SELECT * "
                + "FROM uap_mst_province "
                + "WHERE id = '" + ProvinceId + "' ";
        i.query(sql);

        while (i.resultNext()) {
            c.setProvinceId(i.result().getString("id"));
            c.setProvinceName(i.result().getString("province_name"));

        }

        i.close();
        return c;
    }

    public static Car getColorById(String ColorId) throws Exception {
        Car c = new Car();
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();
        String sql = "SELECT * "
                + "FROM uap_mst_color "
                + "WHERE id = '" + ColorId + "' ";
        i.query(sql);

        while (i.resultNext()) {
            c.setColorId(i.result().getString("id"));
            c.setColorName(i.result().getString("color_name"));

        }

        i.close();
        return c;
    }

    public static Car getBrandById(String BrandId) throws Exception {
        Car c = new Car();
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();
        String sql = "SELECT * "
                + "FROM uap_mst_brand "
                + "WHERE id = '" + BrandId + "' ";
        i.query(sql);

        while (i.resultNext()) {
            c.setBrandId(i.result().getString("id"));
            c.setBrandName(i.result().getString("brand_name"));
        }

        i.close();
        return c;
    }

    public static Car getCarTypeById(String CarTypeId) throws Exception {
        Car c = new Car();
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();
        String sql = "SELECT * "
                + "FROM uap_mst_cartype "
                + "WHERE id = '" + CarTypeId + "' ";
        i.query(sql);

        while (i.resultNext()) {
            c.setCarTypeId(i.result().getString("id"));
            c.setCarTypeName(i.result().getString("cartype_name"));

        }

        i.close();
        return c;
    }

    public static List<Car> getCarTypeList() throws Exception {
        List<Car> list = new ArrayList<Car>();
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();
        String sql = "SELECT * "
                + "FROM uap_mst_cartype c "
                + "WHERE 1";
        i.query(sql);

        while (i.resultNext()) {

            Car c = new Car();
            c.setCarTypeId(i.result().getString("id"));
            c.setCarTypeName(i.result().getString("cartype_name"));
            list.add(c);
        }

        i.close();
        return list;
    }

    public static List<Car> getColorList() throws Exception {
        List<Car> list = new ArrayList<Car>();
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();
        String sql = "SELECT * "
                + "FROM uap_mst_color c "
                + "WHERE 1";
        i.query(sql);

        while (i.resultNext()) {

            Car c = new Car();
            c.setColorId(i.result().getString("id"));
            c.setColorName(i.result().getString("color_name"));
            list.add(c);
        }

        i.close();
        return list;
    }

    public static List<Car> getBrandList() throws Exception {
        List<Car> list = new ArrayList<Car>();
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();
        String sql = "SELECT * "
                + "FROM uap_mst_brand b "
                + "WHERE 1";
        i.query(sql);

        while (i.resultNext()) {

            Car c = new Car();
            c.setBrandId(i.result().getString("id"));
            c.setBrandName(i.result().getString("brand_name"));
            list.add(c);
        }

        i.close();
        return list;
    }

    public static List<Car> getProvinceList() throws Exception {
        List<Car> list = new ArrayList<Car>();
        i = new MySQLJDBC(Config.getMySqlHost(),
                Config.getMySqlUsername(),
                Config.getMySqlPassword(),
                Config.getMySqlDbname());
        i.connect();
        String sql = "SELECT * "
                + "FROM uap_mst_province p "
                + "WHERE 1";
        i.query(sql);

        while (i.resultNext()) {

            Car c = new Car();
            c.setProvinceId(i.result().getString("id"));
            c.setProvinceName(i.result().getString("province_name"));
            list.add(c);
        }

        i.close();
        return list;
    }

    public String getCarTypeId() {
        return carTypeId;
    }

    public void setCarTypeId(String carTypeId) {
        this.carTypeId = carTypeId;
    }

    public String getCarTypeName() {
        return carTypeName;
    }

    public void setCarTypeName(String carTypeName) {
        this.carTypeName = carTypeName;
    }

    public String getColorId() {
        return colorId;
    }

    public void setColorId(String colorId) {
        this.colorId = colorId;
    }

    public String getColorName() {
        return colorName;
    }

    public void setColorName(String colorName) {
        this.colorName = colorName;
    }

    public String getBrandId() {
        return brandId;
    }

    public void setBrandId(String brandId) {
        this.brandId = brandId;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(String provinceId) {
        this.provinceId = provinceId;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public static iDataAdapter getI() {
        return i;
    }

    public static void setI(iDataAdapter i) {
        Car.i = i;
    }


}
